package com.leha.forum.lehare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leha.forum.lehare.entity.Comment;
import com.leha.forum.lehare.entity.Post;
import com.leha.forum.lehare.response.Result;
import com.leha.forum.lehare.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.function.Supplier;

/**
 * @author lovelycat
 * @version 1.0
 * @since 2025-04-16 18:01
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @GetMapping("/post")
    public Result<?> getPostComments(Long postId, Long page) {
        return Result.success("", commentService.getComments(postId, page, 20L));
    }

    @PostMapping("/add")
    public Result<?> addComment(Principal principal, Long postId, Long parentId, String content) {
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) principal;

        if (commentService.addComment(((Long) token.getPrincipal()), postId, parentId, content)) {
            return Result.success("添加成功", null);
        } else {
            return Result.failed(401, "添加失败");
        }
    }
}
